package com.springcloud.providers.controller;

import org.slf4j.*;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@RestController
public class DiscoverController {
    private Logger logger = LoggerFactory.getLogger(getClass());


    @Resource
    private DiscoveryClient discoveryClient;

    @GetMapping("/providers/discovery")
    public Object discover() {
        List<String> services = discoveryClient.getServices();
        for(String el: services) {
            logger.info("======>>> service :" + el );
        }
        List<ServiceInstance> instances = discoveryClient.getInstances("spring-cloud-providers:8001");
        for (ServiceInstance instance : instances) {
            logger.info(instance.getServiceId()+"\t"+instance.getHost()+"\t"+instance.getPort()+"\t"+instance.getUri());
        }
        return this.discoveryClient;

    }
}
